import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import * as path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  server: {
    host: '0.0.0.0',
    port: 3000,
    open: true, // 自动打开浏览器
    cors: true,
    // 代理设置（实际项目中根据后端API地址进行配置）
    proxy: {
      // 代理到后端API
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        secure: false,
        // 将/api开头的请求转发到后端，并去掉/api前缀
        // 例如：/api/binding/list -> http://localhost:8080/binding/list
        rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          // 添加详细的代理日志
          proxy.on('error', (err, req, res) => {
            console.error('代理错误:', err);
          });
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log(`代理请求: ${req.method} ${req.url} -> ${options.target}${proxyReq.path}`);
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log(`代理响应: ${proxyRes.statusCode} ${req.method} ${req.url}`);
          });
        }
      }
    }
  },
  // 自定义启动和构建信息
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    // 构建时清除目标目录
    emptyOutDir: true,
    // 生产环境移除 console 和 debugger
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  }
}) 